﻿using System;
using System.Drawing;
using Domain;
using Domain.Entity;

namespace Spider.Spiders.Workers
{
    internal partial class QqWBSprider : Spider
    {
        internal static string VerifyCode { get; set; }
        internal static Image VerifyCodeImage { get; set; }
        internal static bool AutoWritVerifyCode { get; set; }//自动识别验证码
        internal static int AutoWritVerifyCodeErrorTimes { get; set; }//自动识别验证码错误次数，单个账号
        protected int _currentAcountVerifyCodeErrorTimes = 2;//当前QQ账号自动识别验证码错误次数，单个账号
        private static bool _hasvisitedDestinationUrl = false;//是否当前账号已经访问过指定地址
        internal static bool EnableSayGood { get; set; }//启用点赞
        internal static bool EnableAddFriend { get; set; }//启用加好友
        internal static bool EnableOneAcountAccess { get; set; }//启用单账号访问
        internal static bool EnableSearchUser { get; set; }//启用用户搜索页
        internal static string VisiteDestinationQQWB { get; set; }//启用访问自己指定QQ

        public override void StartToWork(ToDoData data)
        {
            try
            {
                string finnalurl = GetFinalUrl(data.PageUrl, data.DataSource);
                PageData pageData = new PageData();
                switch (data.DataSource)
                {
                    case PageDataSource.QQNumberForWb:
                        pageData = VisiteQqWB(finnalurl);
                        break;
                    case PageDataSource.QQAcountForPostAdd:
                        pageData = QqWBPostAd(data.Obj);
                        break;
                }
                if (pageData != null)
                {
                    pageData.PageUrl = data.PageUrl;
                    PageRepository.AddPage(pageData);
                }
            }
            catch (Exception ex)
            {
                SpiderCaptain.GetInstance().StopToWork(ex.ToString());
                CustomsLog.WriteExceptionErrorLog("QQ微博爬虫工作异常", ex);
            }

        }

       

        /// <summary>
        /// 访问QQ微博
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        private PageData VisiteQqWB(string qq)
        {
            QQAcount acount = DealLoginQQWbByZone();
            SearchBordercastOrUser(qq, acount.QQ);
            if (EnableAddFriend)
                WbAddFriend();
            if (EnableSayGood)
                WbSayGood();


            //访问指定地址
            if (!_hasvisitedDestinationUrl && !string.IsNullOrWhiteSpace(VisiteDestinationQQWB) && (acount.MaxVisiteTimesForWB - 1) == acount.VisitedTimesForWB)
            {
                SearchBordercastOrUser(VisiteDestinationQQWB, acount.QQ);
                _hasvisitedDestinationUrl = true;
                if (EnableAddFriend)
                    WbAddFriend();
                if (EnableSayGood)
                    WbSayGood();


            }

            return null;
        }
        /// <summary>
        /// QQ微博发表广告
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        private PageData QqWBPostAd(Object qqacount)
        {
            DealLoginQQZoneForPostAd(qqacount);
            
            return null;
        }
        
    }
}
